package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.missilike.biz.IMilAccountService;
import com.missilike.biz.IMilPhotoService;
import com.missilike.domain.response.RewardRecordInfo;
import com.missilike.repository.entity.MilAccount;
import com.missilike.repository.entity.MilRewardRecord;
import com.missilike.repository.mapper.MilRewardRecordMapper;
import com.missilike.biz.IMilRewardRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kin
 * @since 2019-01-17
 */
@Service
public class MilRewardRecordServiceImpl extends ServiceImpl<MilRewardRecordMapper, MilRewardRecord> implements IMilRewardRecordService {
    private IMilAccountService milAccountService;

    @Autowired
    private IMilPhotoService photoService;


    @Override
    public void setAccountService(IMilAccountService accountService){
        this.milAccountService=accountService;
    }

    @Override
    public List<RewardRecordInfo> getRewardsForContent(Long id) {
        List<RewardRecordInfo> recordInfos = new ArrayList<>();
        QueryWrapper<MilRewardRecord> rewardRecordQueryWrapper = new QueryWrapper<>();
        rewardRecordQueryWrapper.lambda().eq(MilRewardRecord::getRewardContentId, id);
        List<MilRewardRecord> rewardRecords =  baseMapper.selectList(rewardRecordQueryWrapper);
        return list(rewardRecords);
    }

    @Override
    public List<RewardRecordInfo> getRewardDetail(String id) {
        QueryWrapper<MilRewardRecord> rewardRecordQueryWrapper = new QueryWrapper<>();
        rewardRecordQueryWrapper.eq("reward_account_id", id);
        List<MilRewardRecord> records = baseMapper.selectList(rewardRecordQueryWrapper);
        return list(records);
    }

    private List<RewardRecordInfo> list(List<MilRewardRecord> records){
        List<RewardRecordInfo> infoList = new ArrayList<>();
        if (records.size() > 0) {
            records.forEach(item -> {
                RewardRecordInfo info = new RewardRecordInfo();
                MilAccount account = milAccountService.getById(item.getAccountId());
                info.setAccount(account.getAccount());
                info.setAccountId(item.getAccountId());
                info.setContentId(item.getRewardContentId());
                info.setFaceUrl(photoService.getPhotoUrl(account.getFaceUrlId()));
                info.setCreateTime(item.getCreateTime());
                info.setRewardBalance(item.getRewardBalance());
                infoList.add(info);
            });
            //infoList.stream().sorted(Comparator.comparing(RewardRecordInfo::getCreateTime).reversed()).collect(Collectors.toList());
        }
        return infoList.stream().sorted(Comparator.comparing(RewardRecordInfo::getCreateTime).reversed()).collect(Collectors.toList());
    }
}
